<!--头部公共部分-->
{include file="common/header" /}
<div style="padding:15px;">
    <form class="layui-form layui-form-pane">

        <div class="layui-form-item">

            <div class="layui-inline">
                <!--
                <select name="campus" lay-search="">
                    <option value="">校区选择</option>
                    {volist name="campuslist" id="campus"}
                    <option value="{$campus.id}">{$campus.campus_name}</option>
                    {/volist}
                </select>
                -->
                <script>
                    document.write('<select name="campus" lay-verify="required" disabled>');
                    document.write('<option value=""></option>');
                    document.write('{foreach name="campuslist" id="vo"}');
                    var id = parseInt("{$vo.id}");
                    var campus_id = parseInt("{$res_contract.campus_id}");
                    if(id === campus_id){
                        document.write('<option value="{$vo.id}" selected>{$vo.campus_name}</option>');
                    }else{
                        document.write('<option value="{$vo.id}">{$vo.campus_name}</option>');
                    }

                    document.write('{/foreach}');
                    document.write('</select>');
                </script>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">订单编号</label>
                <label class="layui-form-label" style="width: auto" id="contract_number"><font  color="#46A3FF">订单编号</font></label>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">订单应收</label>
                <label class="layui-form-label" id="total_amount"><font  color="#46A3FF">订单应收</font></label>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">项目总收款</label>
                <label class="layui-form-label" id="amount"><font  color="#46A3FF">{$amount}</font></label>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">待分配金额</label>
                <label class="layui-form-label" id="signing_date"><font  color="#46A3FF">{$stay_distributed}</font></label>
            </div>


        </div>
        <table class="layui-table" id="datalist" lay-filter="Event"></table>
        <script type="text/html" id="barTable">
            <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="allocation_fund">资金分配</a>
            <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="return_fund">资金回抽</a>
        </script>

        <div class="layui-collapse" style="margin-bottom: 10px;" lay-accordion>
            <div class="layui-colla-item">
                <h2 class="layui-colla-title">查看分配记录</h2>
                <div class="layui-colla-content">
                    <table class="layui-table" id="datalistrecord" lay-filter="Event"></table>
                </div>
            </div>
        </div>



        <div class="layui-form-item">
            <div class="layui-input-block">
                <input class="layui-btn  layui-btn-danger" id="cancel" lay-submit lay-filter="form" value="关闭" readonly>
            </div>
        </div>
    </form>
</div>
<!--底部公共部分-->
{include file="common/footer" /}

<script>
    var amount = '{$amount}';  //已收款金额
    var stay_distributed = '{$stay_distributed}';  //待分配金额
    layui.use(['table', 'laydate','element', 'jquery','form','layer'], function(){
        var $ = layui.jquery
            ,element = layui.element
            ,layer = layui.layer
            ,form = layui.form;

        var table = layui.table,
            laydate = layui.laydate,
            $ = layui.$;
        let loading = layer.load();
        //方法级渲染
        var contract_number  = '{$number}';  //获取订单号
        var tableIns = table.render({
            elem: '#datalist',
            url: '{:url("contract/getdistribution")}?number='+ contract_number,
            method: 'POST',
            loading: true,
            parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },

            cols: [[
                {field: 'project_name', title: '项目名称',style:'font-weight: bold;',  fixed: false, align: 'center'},
                {field: 'project_nums', title: '签约数量', style:'font-weight: bold;',  align: 'center'},
                {field: 'project_total_amount', title: '应收金额', style:'font-weight: bold;', align: 'center'},
                {field: 'project_paid_nums', title: '到款数量',style:'font-weight: bold;',  align: 'center'},
                {field: 'project_paid_amount', title: '到款金额',style:'font-weight: bold;',  align: 'center'},
                {field: 'project_surplus_nums', title: '剩余数量', style:'color:red;font-weight: bold;',  align: 'center'},
                {field: 'project_surplus_amount', title: '剩余金额',style:'font-weight: bold;',   align: 'center'},
                {title: '操作', fixed: 'right',  align: 'center', width: 200, toolbar: '#barTable'},
            ]],
            page: true,
            height: 'full-200',
            done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                layer.close(loading);
            }
        });

        //分配记录
        var tableInsrecord = table.render({
            elem: '#datalistrecord',
            url: '{:url("contract/capitalrecord")}?number='+ contract_number,
            method: 'POST',
            loading: true,
            parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },

            cols: [[
                // {type: 'checkbox'},
                {field: 'capital_allocation_id',style:'font-weight: bold;', title: '资金分配ID', sort: true, fixed: false, align: 'center'},
                {field: 'create_time', style:'font-weight: bold;', title: '分配时间',  align: 'center'},
                {field: 'campus_name',style:'font-weight: bold;',  title: '分配校区',  align: 'center'},
                {field: 'distributive_person_text',style:'font-weight: bold;', title: '分配人',  align: 'center'},
                {field: 'project_name',style:'font-weight: bold;', title: '项目名称',  align: 'center'},
                {field: 'allocation_fund',style:'font-weight: bold;color:red;', title: '分配资金',  align: 'center'},
                {field: 'allotted_quantity', style:'font-weight: bold; color:red;',title: '分配数量',  align: 'center'},
            ]],
            page: true,
            height: 'full-150',
            done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                layer.close(loading);
            }
        });


        //监听工具条
        table.on('tool(Event)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值

            var campus_id = $('select[name=campus] option:selected').val();
            if(campus_id == ""){
                layer.msg("请选择校区");
                return;
            }
            if (layEvent === 'allocation_fund') {  //资金分配
                if(data.project_stay_distributed <= 0){
                    layer.msg("待分金额为0不足以资金分配");
                    return;
                }

                var url = '{:url("contract/allocationfund")}?project_id=' + data.project_id + '&project_type=' + data.project_type + '&contract_number='+ contract_number + "&amount=" + amount + "&stay_distributed=" + stay_distributed  + '&campus_id='+ campus_id;
                layer.open({
                    type: 2,
                    title: '资金分配',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['70%', '90%'],
                    content: url,
                    success:function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        body.find("#already_allocated").html('<font  color="#46A3FF">'+ data.project_nums+'|' +data.project_total_amount+'元'+'</font>');
                        body.find("#surplus").html('<font  color="#46A3FF">'+ data.project_paid_nums+'|' +data.project_paid_amount+'元' +'</font>');
                        body.find("#receivables").html('<font  color="#46A3FF">'+ amount +'元'+'</font>');
                        body.find("#wait_divided").html('<font  color="#46A3FF">'+ stay_distributed +'元' +'</font>');
                    }
                });

            } else if (layEvent === 'return_fund') { //资金回抽
                if(data.project_surplus_amount <= 0){
                    layer.msg("金额为0不足以回抽资金");
                    return;
                }

                var url = '{:url("contract/returnfund")}?project_id=' + data.project_id + '&project_type=' + data.project_type +'&project_surplus_amount=' + data.project_surplus_amount + '&contract_number='+ contract_number + '&campus_id='+ campus_id;
                layer.open({
                    type: 2,
                    title: '资金回抽',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['70%', '70%'],
                    content: url,
                    success:function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        body.find("#already_allocated").html('<font  color="#46A3FF">'+ data.project_paid_nums+'|' +data.project_paid_amount+'元'+'</font>');
                        body.find("#surplus").html('<font  color="#46A3FF">'+ data.project_surplus_amount+'元' +'</font>');
                    }
                });
            }
        });


        $('#cancel').click(function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
            parent.location.reload();
        });
    });

</script>